# Process this file with autoconf to produce a configure script.
################################################################
AC_PREREQ(2.65)
AC_INIT([screencasting-teaching-system], [0.1], [jiri.novak@petriny.net], [screencasting-teaching-system])
AC_CONFIG_HEADER([config.h])
AC_CONFIG_AUX_DIR([scripts/config.aux])
AM_INIT_AUTOMAKE

# Checks for programs.
######################
AC_LANG_CPLUSPLUS
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CPP
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

# Checks for header files
#########################
# BSD sockets
AC_CHECK_HEADER(sys/types.h,  , AC_MSG_ERROR([+Could not find required header of BSD sockets: sys/types.h]))
AC_CHECK_HEADER(sys/socket.h, , AC_MSG_ERROR([+Could not find required header of BSD sockets: sys/socket.h]))
AC_CHECK_HEADER(sys/un.h,     , AC_MSG_ERROR([+Could not find required header of BSD sockets: sys/un.h]))
AC_CHECK_HEADER(netinet/in.h, , AC_MSG_ERROR([+Could not find required header of BSD sockets: netinet/in.h]))
AC_CHECK_HEADER(netdb.h,      , AC_MSG_ERROR([+Could not find required header of BSD sockets: header netdb.h]))
AC_CHECK_HEADER(unistd.h,     , AC_MSG_ERROR([+Could not find required header of BSD sockets: arpa/inet.h]))

# Pthread
AC_CHECK_HEADER(pthread.h,    , AC_MSG_ERROR([+Could not find required header of pthreads: pthread.h]))

# Checks for libraries and frameworks.
######################################
# BSD sockets
# AC_CHECK_LIB(nsl, gethostbyname, , AC_MSG_ERROR([+Could not find function gethostbyname in nsl library]))

# Pthread
# AC_CHECK_LIB(pthread, pthread_create, , AC_MSG_ERROR([+Could not find function pthread_create in pthread library]))

# GTKmm & Glademm
PKG_CHECK_MODULES(GTKMM, gtkmm-2.4 >= 2.8, HAVE_GTKMM=yes, AC_MSG_ERROR([+Could not find gtkmm-2.4]))
PKG_CHECK_MODULES(GLADEMM, libglademm-2.4 >= 2.6, HAVE_GLADEMM=yes, AC_MSG_ERROR([+Could not find libglademm-2.4]))

# Log4cxx
PKG_CHECK_MODULES(LOG4CXX, liblog4cxx >= 0.10 apr-util-1 apr-1, HAVE_LOG4CXX=yes, AC_MSG_ERROR([+Could not find log4cxx (liblog4cxx10-dev, libaprutil-1, libapr-1)]))

# FFMPEG
PKG_CHECK_MODULES(FFMPEG, libavutil libavformat libavcodec libswscale libavdevice libavfilter, HAVE_FFMPEG=yes,
		  AC_MSG_ERROR([ +Could not find ffmpeg (libavutil libavformat libavcodec libswscale libavdevice libavfilter) - install libavutil52-dev libavformat52-dev libavcodec52-dev libavdevice52-dev libavfilter52-dev libswcale-dev]))
AC_CHECK_LIB(avcodec, avcodec_decode_video2,, AC_MSG_ERROR([ +Your version of libavcodec is too old ]))
AC_CHECK_LIB(avcodec, avcodec_decode_audio3,, AC_MSG_ERROR([ +Your version of libavcodec is too old ]))

# Alsa
PKG_CHECK_MODULES(ALSA, alsa >= 1.0.0, HAVE_ALSA=yes, AC_MSG_ERROR([+Could not find alsa - install libasound2-dev]))

# XML
PKG_CHECK_MODULES(LIBXML, libxml-2.0 >= 2.5 libxml++-2.6 >= 2.10.0, HAVE_XML=yes, AC_MSG_ERROR([+Could not find xml (libxml-2.0 libxml++-2.6) - install libxml-2.0-dev libxml++-2.6-dev]))

# SDL
PKG_CHECK_MODULES(SDL, sdl >= 1.2, HAVE_SDL=yes, AC_MSG_ERROR([+Could not find sdl - install libsdl1.2-dev]))

AC_SUBST(GTKMM_CFLAGS)
AC_SUBST(GTKMM_LIBS)
AC_SUBST(GLADEMM_CFLAGS)
AC_SUBST(GLADEMM_LIBS)
AC_SUBST(LOG4CXX_CFLAGS)
AC_SUBST(LOG4CXX_LIBS)
AC_SUBST(FFMPEG_CFLAGS)
AC_SUBST(FFMPEG_LIBS)
AC_SUBST(ALSA_CFLAGS)
AC_SUBST(ALSA_LIBS)
AC_SUBST(LIBXML_CFLAGS)
AC_SUBST(LIBXML_LIBS)
AC_SUBST(SDL_CFLAGS)
AC_SUBST(SDL_LIBS)

# Checks for header files.
##########################
AC_HEADER_STDC

# Debug mode.
#############
AC_ARG_ENABLE(debug,
   [  --enable-debug          Build in debug mode ],
   DEBUG=$enableval)

AM_CONDITIONAL(DEBUG, test "$enable_debug" = "yes")
if test "$DEBUG" = "yes"; then
    AC_DEFINE(ENABLE_DEBUG, [], "Enable debug mode")
    CXXFLAGS="$CXXFLAGS -g -O1 -Wall" # -Werror -Wfatal-errors"
fi

# Unit tests.
#############
AC_ARG_ENABLE(unittests,
   [  --enable-unittests      Enables build of unittests],
   ENABLE_UNITTEST=$enableval)

if test "$ENABLE_UNITTEST" = "yes"; then
    AC_CHECK_HEADERS(
    [gtest/gtest.h],,
    [AC_MSG_ERROR([Missing gtest library, install the google unittest framework - libgtest-dev])])

    UNITTEST_LIBS="$(gtest-config --libs)"
    AC_SUBST(UNITTEST_LIBS)
fi
AM_CONDITIONAL(ENABLE_UNITTEST, test "$enable_unittests" = "yes")

# Specify output files.
#######################
AC_CONFIG_FILES([Makefile
	src/Makefile
        src/common/Makefile
        src/client/Makefile          
        src/server/Makefile
])
AC_OUTPUT

# Configuration summary
#######################
echo
echo "CONFIGURATION SUMMARY ----"

if test "x$ENABLE_UNITTEST" = xyes; then
    echo "unittests           : enabled"
else
    echo "unittests           : disabled"
fi

if test "x$DEBUG" = xyes; then
    echo "debug mode          : enabled"
else
    echo "debug mode          : disabled"
fi
